< previous page page_486 next page >

Page 486
9f31265bb40b592b1929e18a47ab28e5.gif
   oRS.Open strSQL, goConn, _
            adOpenForwardOnly, _
            adLockReadOnly, _
            adCmdText

   lstNames.Clear
   Do Until oRS.EOF
      lstNames.AddItem oRS!szLast_nm & _
                       ", " & oRS("sFirst_nm")

      oRS.MoveNext
   Loop
   oRS.Close
End Sub
In the Click event of the ForwardOnly command button, you dim an ADO recordset. You then create a SQL SELECT statement so you can pass that to the Open method of the Recordset object. You then use the New keyword to create a new instance of the Recordset class. You now pass to the Open method the SQL string, the global Connection object you created in Sub Main(), the cursor type, the lock type, and the option that tells the Open method what the source is (in this case, a SQL statement).
After opening the recordset, you loop through it until you hit the end of the file. Each time through the loop, you can grab data from the recordset by specifying the name of the Recordset object, followed by the bang operator (!), followed by the field name:
9f31265bb40b592b1929e18a47ab28e5.gif
oRS!szLast_nm
You can also retrieve data from a specific field by enclosing the field name in quotes and passing that to the Recordset object:
9f31265bb40b592b1929e18a47ab28e5.gif
oRS("sFirst_nm")
Opening a Connection and Recordset Together
In the previous example, you passed the public Connection object you created to the Recordset object as the second parameter. If you want to open another connection for a particular recordset, you can just pass a connection string as the second parameter. Heres how:

 
< previous page page_486 next page >

If you like this book, buy it!